﻿
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_ManagerLayout.cshtml";
}

@section style{
<style>
    th, td{
        text-align:center;
    }
</style>    
}
<div class="tabbable" id="tabs">
    <ul class="nav nav-tabs">
        <li class="active">
            <a href="#panel" data-toggle="tab"  onclick="loadOrders(-1, 1)">全部</a>
        </li>
        <li>
            <a href="#panel" data-toggle="tab" onclick="loadOrders(2, 1)">待发货</a>
        </li>
        <li>
            <a href="#panel" data-toggle="tab" onclick="loadOrders(3, 1)">待收货</a>
        </li>
        <li>
            <a href="#panel" data-toggle="tab" onclick="loadOrders(4, 1)">已完成</a>
        </li>
    </ul>
    <div class="tab-content">
        <div class="tab-pane active" id="panel">
            <table id="orderList" class="table table-striped">
                <thead>
                    <tr>
                        <th style="width:50%;">商品名称</th>
                        <th style="width:10%">买家</th>
                        <th style="width:5%">单价</th>
                        <th style="width:5%">数量</th>
                        <th style="width:10%">成交价</th>
                        <th style="width:10%">状态</th>
                        <th style="width:10%">操作</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
            <ul id="paging" class="pagination">
            </ul>
        </div>
    </div>
</div>

@section script{
<script>
    var _state = 0;
    var _page = 0;

    $(function () {
        $("#Index").addClass("active");
        loadOrders(-1, 1);
    });

    function loadOrders(state, page) {
        if (state != _state || page != _page) {
            if (page == 0) page = 1;//预留强制刷新
            _state = state;
            $.ajax({
                type: "POST",
                url: "/Manager/GetOrders",
                data: { state: state, page: page },
                dataType: "json",
                success: function (data) {
                    loadTable(data.List);
                    loadPaging(data);
                }
            });
        }
    }

    function loadTable(list) {
        $('#orderList tbody').empty();
        for (var i in list) {
            var row = "<tr>"
                + "<td style=' text-align:left'><a href='/Manager/Detail?id=" + list[i].Id + "'>" + list[i].CommodityName + "</a></td>"
                + "<td>" + list[i].UserName + "</td>"
                + "<td>" + list[i].UnitPrice + "</td>"
                + "<td>" + list[i].Number + "</td>"
                + "<td>" + list[i].Price + "</td>"
                + "<td>" + list[i].StateText + "</td>"
                + "<td>"
                + (list[i].State == 2 ? "<a href='javascript:void(0)' onclick = 'deliver(\"" + list[i].Id + "\")' class='btn btn-default'>发货<a>" : "")
                + "</td>"
                + "</tr>";
            $('#orderList tbody').append(row);
        }
    }

    function loadPaging(paging) {
        _page = paging.Page;
        $('#paging').empty();
        var lis = "<li><a href='javascript:void(0)' onclick='loadOrders(" + _state + ", 1)'>首页</a></li>";

        for (var i = 1; i <= paging.PageNumber; i++) {
            lis += "<li><a href='javascript:void(0)' onclick = 'loadOrders(" + _state + ", " + i + ")'>" + i + "</a></li>";
        }

        lis += "<li><a href='javascript:void(0)' onclick='loadOrders(" + _state + ", " + paging.PageNumber + ")'>尾页</a></li>";
        $('#paging').append(lis);
    }

    function deliver(id) {
        if (confirm("确认发货吗？")) {
            $.ajax({
                type: "POST",
                url: "/Manager/Deliver",
                data: { id: id},
                dataType: "json",
                success: function (data) {
                    loadOrders(_state, 0) //0页 强制刷新
                }
            });
        }
    }
</script>
}

